package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.VocBookConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapBookDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapBookMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant.*;

@Service
public class VocBookServiceImpl implements VocBookService {

    @Autowired
    VocBookMapper vocBookMapper;

    @Autowired
    VocChapBookMapper vocChapBookMapper;

    @Autowired
    VocBookConverter vocBookConverter;

    // 分页展示单词书籍(后端-物料管理-书籍管理)
    @Override
    public PageDTO<VocBookDTO> getBookPage(VocBookPageRequest pageQuery) {

        // 检查分页参数,如果没有指定分页参数，查询所有的书籍
        if (pageQuery.getPageNum() == null) {
            // 根据书籍类目，查询所有的书籍
            LambdaQueryWrapper<VocBookDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VocBookDO::getBcId, pageQuery.getCategoryId());
            List<VocBookDO> vocBookDOS = vocBookMapper.selectList(queryWrapper);
            PageDTO<VocBookDTO> vocBookDTOPageDTO = vocBookConverter.vocBookList2Page(vocBookDOS, (long) vocBookDOS.size());
            return vocBookDTOPageDTO;
        }

        // 有分页参数，查询分页数据
        // 构造查询条件
        LambdaQueryWrapper<VocBookDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocBookDO::getBcId, pageQuery.getCategoryId())
                .like(pageQuery.getKeyword() != null,VocBookDO::getName, pageQuery.getKeyword());

        // 构造分页对象
        Page<VocBookDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());

        // 执行查询
        Page<VocBookDO> resultPage = vocBookMapper.selectPage(page, queryWrapper);

        List<VocBookDO> records = resultPage.getRecords();
        long total = resultPage.getTotal();

        PageDTO<VocBookDTO> vocBookDTOPageDTO = vocBookConverter.vocBookList2Page(records, total);
        return vocBookDTOPageDTO;
    }

    // 添加单词书籍(后端-物料管理-书籍管理)
    @OperateLog(dataType = "书籍管理", operateType = OPERATE_ADD)
    @Override
    public Long addVocBook(VocBookCommand command) {

        // 判断要添加的书籍是否存在。其实老师的也没有去重功能，可不加
        LambdaQueryWrapper<VocBookDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocBookDO::getName, command.getName());
        VocBookDO vocBookDO = vocBookMapper.selectOne(queryWrapper);
        if (vocBookDO != null) {
            throw new BusinessException(ResultCodeEnum.BOOK_EXISTED);
        }

        VocBookDO vocBookDO1 = vocBookConverter.vocBookCommand2VocBookDO(command);
        vocBookMapper.insert(vocBookDO1);

        // toDo：这里是要返回书籍的id吗？
        return vocBookDO1.getId();
    }

    // 更新单词书籍(后端-物料管理-书籍管理)
    @OperateLog(dataType = "书籍管理", operateType = OPERATE_UPDATE)
    @Override
    public void updateVocBook(VocBookCommand command) {
        // 根据id，判断要修改的书籍是否存在
        // 其实可以不用判断，因为如果没有这条数据，前端也不会出现对应的编辑按钮，所以不会出现修改不存在的书籍的情况
        LambdaQueryWrapper<VocBookDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocBookDO::getId, command.getId());
        VocBookDO vocBookDO = vocBookMapper.selectOne(queryWrapper);
        if (vocBookDO == null) {
            throw new BusinessException(ResultCodeEnum.BOOK_NOT_EXISTED);
        }

        // 书籍存在，修改书籍信息
        VocBookDO vocBookDO1 = vocBookConverter.vocBookCommand2VocBookDO(command);
        vocBookDO1.setId(Long.parseLong(command.getId()));
        vocBookMapper.updateById(vocBookDO1);
    }

    // 删除单词书籍(后端-物料管理-书籍管理)
    @OperateLog(dataType = "书籍管理", operateType = OPERATE_SINGLE_DELETE)
    @Override
    public DeleteBookOperateDTO deleteVocBook(Long bookId) {
        VocBookDO vocBookDO = vocBookMapper.selectById(bookId);
        DeleteBookOperateDTO deleteBookOperateDTO = vocBookConverter.vocBookDO2DeleteBookDTO(vocBookDO);

        // todo:删除 表voc_chapter_book 数据
        LambdaQueryWrapper<VocChapBookDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocChapBookDO::getBookId, bookId);
        vocChapBookMapper.delete(queryWrapper);

        // todo:删除 表voc_book 数据
        vocBookMapper.deleteById(bookId);

        return deleteBookOperateDTO;
    }

    // 批量删除选中的单词书籍(后端-物料管理-书籍管理)
    @OperateLog(dataType = "书籍管理", operateType = OPERATE_BATCH_DELETE)
    @Override
    public List<DeleteBookOperateDTO> deleteVocBooks(BatchDeleteCommand command) {
        // 获取要删除的书籍信息列表
        List<String> ids = command.getIds();
        List<VocBookDO> vocBookDOS = vocBookMapper.selectByIds(ids);
        List<DeleteBookOperateDTO> deleteBookOperateDTOS = vocBookConverter.vocBookDOs2DeleteBookDTOs(vocBookDOS);

        // todo:删除 表voc_chapter_book 数据
        LambdaQueryWrapper<VocChapBookDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(VocChapBookDO::getBookId, ids);
        vocChapBookMapper.delete(queryWrapper);

        // todo:删除 表voc_book 数据
        vocBookMapper.deleteByIds(ids);

        return deleteBookOperateDTOS;
    }
}
